﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using ECCIT.Business.Model;

using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Configuration;

public partial class admin_infomation_info_preview : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersModel um = GetSessionUsersModel();
            try
            {
                string columnID = Request.Params["cid"];
                string html = null;
                if (string.IsNullOrEmpty(columnID))
                {
                    string aid = Request.Params["aid"];
                    CmsVo vo = new CmsVo();
                    vo.Aid = int.Parse(aid);
                    vo.Preview = 1;

                    IGenerateHtml bll = new CmsArticleBll();
                    IStaticGeneratableModel articleModel = bll.GetModelByID(vo.Aid);
                    html = bll.GetTemplateType(articleModel).GetHtml();
                }
                else
                {
                    string langID = Request.Params["langID"];
                    string id = Request.Params["id"];
                    IGenerateHtml bll = GenerateHtmlFactory.CreateInstance(int.Parse(columnID), int.Parse(langID));
                    if (bll != null)
                    {
                        IStaticGeneratableModel model = bll.GetModelByID(int.Parse(id));
                        html = bll.GetTemplateType(model).GetHtml();
                    }
                }

                #region 2012-6-1 解决include的浏览的问题，因为不想改原来的生成结构，这里用的正责进行了处理 李学明              
                var include = new Regex("<!--#include   file=\"/uploadfiles/htmlfile/*.*\"-->");
                if (html != null)
                {
                    Match m = include.Match(html);
                    if (m.Success)
                    {
                        var result = m.Value;
                        var arr = result.Split('/');
                        if (arr.Length == 4)
                        {
                            var filename = arr[3].Replace("\"-->", string.Empty);
                            var sr = new StreamReader(this.Server.MapPath("/") + "/uploadfiles/htmlfile/" + filename,
                                                      Encoding.Default);
                            string includecontent = sr.ReadToEnd();
                            sr.Close();
                            if (!string.IsNullOrEmpty(includecontent))
                            {
                                html = html.Replace(m.Value, includecontent);
                            }
                        }

                    }
                }
                #endregion

                Response.Write(html);
            }
            catch (Exception ex)
            {
                Response.Write("Error occurred while previewing page. Error: " + ex.Message);
            }
        }
    }
}
